package com.epfresh.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.widget.MsgView;
import com.epfresh.bean.Category;
import com.epfresh.bean.ProductCategory;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class DeliveryStoreCategoryAdapter extends BaseExpandableListAdapter implements View.OnClickListener {

    public Context context;

    private ExpandableListView listView;


    private List<Category> list;
    private AdapterView.OnItemClickListener onItemClickListener;

    public DeliveryStoreCategoryAdapter(Context context, List<Category> list) {
        this.context = context;
        this.list = list;
    }

    public ExpandableListView getListView() {
        return listView;
    }

    public void setListView(ExpandableListView listView) {
        this.listView = listView;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public void show(MsgView msgView, int num, boolean isShow) {
        if (msgView == null) {
            return;
        }
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) msgView.getLayoutParams();
        DisplayMetrics dm = msgView.getResources().getDisplayMetrics();
        msgView.setVisibility(View.VISIBLE);
        if (num <= 0) {//圆点,设置默认宽高
            msgView.setStrokeWidth(0);
            msgView.setText("");

            lp.width = (int) (4 * dm.density);
            lp.height = (int) (4 * dm.density);
            msgView.setLayoutParams(lp);
            if (!isShow) {
                msgView.setVisibility(View.GONE);
            }
        } else {
            lp.height = (int) (15 * dm.density);
            if (num > 0 && num < 10) {//圆
                lp.width = (int) (15 * dm.density);
                msgView.setText(num + "");
            } else if (num > 9 && num < 100) {//圆角矩形,圆角是高度的一半,设置默认padding
                lp.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
                msgView.setPadding((int) (5 * dm.density), 0, (int) (5 * dm.density), 0);
                msgView.setText(num + "");
            } else {//数字超过两位,显示99+
                lp.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
                msgView.setPadding((int) (5 * dm.density), 0, (int) (5 * dm.density), 0);
                msgView.setText("99+");
            }
            msgView.setLayoutParams(lp);
        }
    }


    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer) {
            int groupPosition = (int) o;
            o = v.getTag(R.id.item_key_status);

            if (o != null && o instanceof Integer){
                int childPosition = (int) o;
                if (childPosition == -1){//点击一级


                    boolean groupExpanded = listView.isGroupExpanded(groupPosition);
                    if (groupExpanded){
                        listView.collapseGroup(groupPosition);
                        updateView(v, groupPosition,-2);
                    }else {
                        updateView(v, groupPosition,-1);
                        for (int i = 0;i<list.size();i++){
                            if ( i == groupPosition) {
                                listView.expandGroup(i);
                            }else {
                                listView.collapseGroup(i);
                            }
                        }
                    }


                }else {//点击二级
                   updateChildView(v,groupPosition,childPosition);
                }
            }

        }

    }

    private void updateView(View v, int position ,int type) {
        for (int i = 0; i < list.size(); i++) {
            Category p = list.get(i);
            if (i == position) {
                p.setSelect(true);
                p.updateChildSelectItem();
            } else {
                p.setSelect(false);
            }
        }

        notifyDataSetChanged();

        if (onItemClickListener != null)
            onItemClickListener.onItemClick(null, v, position, type);
    }

    private void updateChildView(View v, int groupPosition, int position) {
        Category p = list.get(groupPosition);
        p.setChildPosition(position);
        p.updateChildSelectItem();

        notifyDataSetChanged();

        if (onItemClickListener != null)
            onItemClickListener.onItemClick(null, v, groupPosition, position);
    }

    public void setCurrent(int position) {
        updateView(null, position , -1);
    }

    @Override
    public int getGroupCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (list != null && list.size() > groupPosition) {
            Category storeCategoryItem = list.get(groupPosition);
            if (storeCategoryItem != null) {
                List<ProductCategory> children = storeCategoryItem.getItem();
                if (children != null) {
                    return children.size();
                }
            }
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (list != null && list.size() > groupPosition)
            return list.get(groupPosition);
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (list != null && list.size() > groupPosition) {
            Category storeCategoryItem = list.get(groupPosition);
            if (storeCategoryItem != null) {
                List<ProductCategory> children = storeCategoryItem.getItem();

                return children;

            }
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
        TextView tv = null;
        MsgView msgView = null;
//        View vLine = null;
        View vSelect = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_category, null, false);
            tv = (TextView) view.findViewById(R.id.tv_category);
            msgView = (MsgView) view.findViewById(R.id.msg);
//            vLine = view.findViewById(R.id.v_end);
//            vLine.setBackgroundResource(R.color.category_item_bg_2);
            vSelect = view.findViewById(R.id.v_select);
            view.setTag(tv);
//            view.setTag(R.id.item_key_status, vLine);
            view.setTag(R.id.item_key_position, msgView);
            view.setTag(R.id.item_key_status, vSelect);

        } else {
            tv = (TextView) view.getTag();
//            vLine = (View) view.getTag(R.id.item_key_status);
            msgView = (MsgView) view.getTag(R.id.item_key_position);
            vSelect = (View) view.getTag(R.id.item_key_status);
        }
        Category p = list.get(groupPosition);

//        if (groupPosition != 0 && !listView.isGroupExpanded(groupPosition)) {
//            view.findViewById(R.id.v_line).setVisibility(View.VISIBLE);
//        } else {
//            view.findViewById(R.id.v_line).setVisibility(View.GONE);
//        }



        if (p.isSelect()) {
            vSelect.setVisibility(View.VISIBLE);
            tv.setBackgroundResource(android.R.color.white);
//            vLine.setBackgroundResource(android.R.color.white);
            if (listView.isGroupExpanded(groupPosition)){
                tv.setTextColor(Color.parseColor("#434343"));
            }else {
                tv.setTextColor(context.getResources().getColor(R.color.green_little));
            }
        } else {
            vSelect.setVisibility(View.GONE);
            tv.setBackgroundColor(Color.parseColor("#fafafc"));
//            vLine.setBackgroundResource(R.color.category_item_bg_2);
            tv.setTextColor(Color.parseColor("#434343"));
        }

        msgView.setVisibility(View.VISIBLE);
        msgView.setText(p.getCount() +"");
        Log.e("count",p.getCount()+"");
        show(msgView, (int) p.getCount(), false);
        tv.setTag(R.id.item_key_position, groupPosition);
        tv.setTag(R.id.item_key_status, -1);
        tv.setOnClickListener(this);
        tv.setText(p.getName() + "");
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
        TextView tv = null;
        MsgView msgView = null;


        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_category_child, null, false);
            tv = (TextView) view.findViewById(R.id.tv_category);
            msgView = (MsgView) view.findViewById(R.id.msg);

            view.setTag(tv);

            view.setTag(R.id.item_key_position, msgView);
        } else {
            tv = (TextView) view.getTag();

            msgView = (MsgView) view.getTag(R.id.item_key_position);
        }
       Category group = list.get(groupPosition);

        List<ProductCategory> child = group.getItem();


        ProductCategory p = child.get(childPosition);


        if (p.isSelect()) {

            tv.setTextColor(context.getResources().getColor(R.color.green_little));

        } else {
//            tv.setBackgroundResource(R.color.category_item_bg_2);
//            vLine.setBackgroundResource(R.color.category_item_bg_2);
            tv.setTextColor(Color.parseColor("#626262"));

        }
        if (childPosition == child.size() -1 ) {
            view.findViewById(R.id.v_line).setVisibility(View.VISIBLE);
        } else {
            view.findViewById(R.id.v_line).setVisibility(View.GONE);
        }
        msgView.setVisibility(View.VISIBLE);
        msgView.setText(p.getCount() + "");
        show(msgView, (int) p.getCount(), false);
        tv.setTag(R.id.item_key_position, groupPosition);
        tv.setTag(R.id.item_key_status, childPosition);
        tv.setOnClickListener(this);
        tv.setText(p.getName());
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
